package com.sinosoft.dsp.component.bpm.viewbean;

import java.util.Map;

import javax.xml.namespace.QName;

import org.primefaces.json.JSONException;
import org.primefaces.json.JSONObject;

import com.sinosoft.dsp.component.bpm.util.BpmImageType;
import com.sinosoft.dsp.component.bpm.util.DeploymentFileType;
import com.sinosoft.dsp.component.bpm.util.ProcessParse;
import com.sinosoft.dsp.platform.components.portal.iteration.SinoTreeDataObject;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.system.utils.Const;

public class BpmConfigDataObject extends SinoTreeDataObject {

	private static final long serialVersionUID = 5563917588346394132L;

	private static final Log log = Log.getLog(BpmConfigDataObject.class
			.getName());

	private DeploymentFileType fileType;
	private String deploymentId;
	private String resourceName;

	private String largeIcon;

	public void setFileType(DeploymentFileType ft) {
		this.fileType = ft;
		this.setType(ft.getName());
	}

	public DeploymentFileType getFileType() {
		return fileType;
	}

	public String getDeploymentId() {
		return deploymentId;
	}

	public String getResourceName() {
		return resourceName;
	}

	public String getLargeIcon() {
		return largeIcon;
	}

	public void resetLargeIcon() {
		if (this.fileType != null) {
			this.largeIcon = "";

			if (!Const.isEmpty(this.deploymentId)
					&& !Const.isEmpty(this.resourceName)) {
				if (this.fileType == DeploymentFileType.RESOURCE_DIRECTORY) {
					JSONObject dir = ProcessParse.parseDir(deploymentId,
							resourceName);
					if (dir.has(BpmAddEditPackageViewBean.FIELD_PACKAGE_ICON)) {
						this.largeIcon = dir
								.optString(BpmAddEditPackageViewBean.FIELD_PACKAGE_ICON);
					}
					if (Const.isEmpty(this.largeIcon)) {
						this.largeIcon = BpmImageType.defaultPackageIcon;
					}
					this.largeIcon = BpmImageType
							.getPackageIconPath(this.largeIcon);
				} else if (this.fileType == DeploymentFileType.RESOURCE_FILE_JPDL) {
					org.jbpm.xsdobject.Process process = ProcessParse
							.parseJpdlDefine(deploymentId, resourceName);

					Map<QName, String> attrs = process.getOtherAttributes();
					QName icon = QName
							.valueOf(BpmAddEditDefineViewBean.FIELD_DEFINE_ICON);
					if (attrs != null && attrs.containsKey(icon)) {
						this.largeIcon = attrs.get(icon);
					}
					if (Const.isEmpty(this.largeIcon)) {
						this.largeIcon = BpmImageType.defaultDefineIcon;
					}
					this.largeIcon = BpmImageType
							.getDefineIconPath(this.largeIcon);
				}
			}

			if (Const.isEmpty(this.largeIcon)) {
				this.largeIcon = this.fileType.getLargeIcon();
			}
		}
	}

	public void setData(String deploymentId, String resourceName) {
		this.deploymentId = deploymentId;
		this.resourceName = resourceName;
	}

	public String getData() {
		JSONObject json = new JSONObject();
		try {
			json.put("deploymentId", this.deploymentId);
			json.put("resourceName", this.resourceName);
			json.put("name", this.getName());
			json.put("fileType", this.fileType.getName());
			json.put("iconSrc", this.largeIcon);
		} catch (JSONException e) {
			log.error("BPM获得数据出现错误");
		}

		return json.toString();
	}

}
